<?


class UICtrlContainer extends JCAT_Controller
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	array
	 */
	public function UICtrlContainer(JCAT_IController $aParentController, UICtrlContainerDoc $aDoc)
	{
		$aTopController = $aParentController->GetTopController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTopController,'IToolboxCoder',JCAT_Language::Sentence('UICtrlContainer 所在的顶层控制器，必须实现  IToolboxCoder 接口。')) ;
		
		$this->JCAT_Controller(__CLASS__,$aParentController) ;
		$this->Initialize($aDoc) ;
	}


	/**
	 * 初始化相关的 MVC 对象
	 * 
	 * @access	public
	 * @return	void
	 */
	public function Initialize(UICtrlContainerDoc $aDoc)
	{
		// 子控件 视图
		$aView = new UICtrlContainerView($this) ;
		$aView->SetDocument($aDoc) ;
		$this->AddView($aView) ;
		
		// 子 文档
		$this->AddDocument($aDoc,'UICtrlContainerDoc') ;
	}


	/**
	 * 
	 *
	 * @access	public
	 * @return	void
	 */
	public function MainRun($arrArgv=array())
	{
		// 载入 所有 子控件		 
		$aView = $this->GetView('UICtrlContainerView') ;
		$aView->LoadChildrenUICtrl() ;
	}


	/**
	 * 响应 BtnAddIUCtrl 按钮
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnAddIUCtrlClicked()
	{
		// 文件选择对话框
		JCAT_Package::ImportClass('JCAT_GtkFSDialog',JCAT::PACKAGE_MVCGTK) ;
		$arrPaths = JCAT_GtkFSDialog::Choose(
			Gtk::FILE_CHOOSER_ACTION_OPEN
			, null
			, "php	*.php|*.php5|*.pxp|*.phpd"
			, true
			, JCAT_Language::Sentence('请选择 UI控件')
		) ;

		// 用户取消
		if( $arrPaths===null )
		{
			return ;
		}
		
		else 
		{
			foreach ($arrPaths as $sPath)
			{
				$this->LoadUICtrl($sPath) ;
			}
		}
	}
	
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$sUICtrlPath	string			what's this
	 * @return	void
	 */
	public function LoadUICtrl($sUICtrlPath)
	{
		
		// 当前文档路径
		$aTopController = $this->GetTopController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTopController,'IToolboxCoder') ;
		
		$sCurrentDocPath =  $aTopController->GetPath() ;
		if( empty($sCurrentDocPath) )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('在载入UI控件以前，请先确定当前文档的路径。')) ;
			return ;
		}
	
		// 避免 加入 自己
		if( realpath($sCurrentDocPath) == realpath($sUICtrlPath) )
		{
			return ;
		}
		
		// 待加入文档 与 当前文档的相对路径
		$sRelativePath = JCAT_FSO::GetRelativePath($sCurrentDocPath,$sUICtrlPath)  ;
		
		// 恢复 子控件 的 文档
		$aLoadingDoc = CoderDocBase::WakeupDocument($sUICtrlPath) ;
		if( !$aLoadingDoc )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('无法打开文档。')) ;
			return ;
		}
		
		// 加入到文档
		$aDoc = $this->GetDocument('UICtrlContainerDoc') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDoc,'UICtrlContainerDoc') ;

		// 无效的路径
		if( !$aDoc->AddUICtrl($aLoadingDoc,$sRelativePath) )
		{
			return ;
		}

		// 更新视图
		$aView = $this->GetView('UICtrlContainerView') ;
		$aView->LoadChildrenUICtrl() ;

		return ;
	}
	
}

?>